package com.dgshanger.sanhxiaofeisc.ui.recharge

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
import com.dgshanger.sanhxiaofeisc.MainActivity
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_VIP
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_VIP_COUNT
import com.dgshanger.sanhxiaofeisc.entity.HistoryPaidEntity
import com.dgshanger.sanhxiaofeisc.entity.UserEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayForContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.PayForPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_pay_for.*
import kotlinx.android.synthetic.main.item_history_aid_list.view.*
import kotlinx.android.synthetic.main.item_history_aid_list_.view.*


/**
 * PayForActivity
 * 沉迷学习不能自拔
 * Describe：他人代付页码
 * Created by 雷小星🍀 on 2018/4/19 15:04.
 */
class PayForActivity : BaseMvpActivity<PayForContract.Model, PayForContract.View, PayForPresenter>(), PayForContract.View {

    override fun setHistoryData(historyPaidEntityList: MutableList<HistoryPaidEntity>?) {
        view_content.removeAllViews()
        if (historyPaidEntityList == null || historyPaidEntityList.size == 0) {
            val emptyView = createHistoryView(null)
            view_content.addView(emptyView)
            emptyView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }
        for (mEntity in historyPaidEntityList!!) {
            val historyView = createHistoryView(mEntity)
            view_content.addView(historyView)
            historyView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }
    }

    override fun getOtherPayVipQty(): String = intent.getIntExtra(KEY_INTENT_VIP_COUNT, 0).toString()

    override fun setUserData(userData: UserEntity?) {
        ImageLoader.loadAvatar(mContext, iv_avatar, userData?.avatar)
        tv_nick_name.text = userData?.nick_name
    }

    fun createHistoryView(entity: HistoryPaidEntity?): View {
        if (entity == null) {
            //空布局
            return LayoutInflater.from(mContext).inflate(R.layout.item_view_empty, null)
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_history_aid_list_, null)

        with(entity) {
            view.tv_user_name.text = rcvd_user_name
            view.tv_user_mobile.text = rcvd_user_mobile
            ImageLoader.loadAvatar(view.context, view.civ_avatar, rcvd_user_avatar)
        }
        view.setOnClickListener {
            //设置选中人
            et_otherpay_mobile.setText(entity.rcvd_user_mobile)
            ImageLoader.loadAvatar(mContext, iv_avatar, entity.rcvd_user_avatar)
            tv_nick_name.text = entity.rcvd_user_name
        }

        return view
    }

    override fun getOtherPayMobile(): String = et_otherpay_mobile.text.toString()

    override fun getRemark(): String = et_remark.text.toString()

    override fun getOtherpayVipId(): String = intent.getStringExtra(KEY_INTENT_VIP)

    override fun createPresenter(): PayForPresenter = PayForPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_pay_for


    private var mOtherpay_shop_id: String? = null
    private var mPrice: String? = null

    override fun initData() {
        mOtherpay_shop_id = intent?.getStringExtra("otherpay_shop_id")
        mPrice = intent?.getStringExtra("price")
        getPresenter().loadHistory()
    }

    override fun initEvent() {

        et_otherpay_mobile.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                et_otherpay_mobile.setSelection(et_otherpay_mobile.length())
                if (et_otherpay_mobile.length() == 11) {
                    //手机号输入完整时进行查询
                    getPresenter().loadUser(s.toString())
                } else {
                    iv_avatar.setImageResource(R.color.colorDivler)
                    tv_nick_name.text = "_ _"
                }
            }

        })

        btn_sure.setOnClickListener {
            mOtherpay_shop_id
            if (TextUtils.isEmpty(mOtherpay_shop_id)) {
                getPresenter().otherPayApply()
            } else {
                getPresenter().shopOtherPayApply(mOtherpay_shop_id, mPrice)
            }
        }
    }

    override fun onOtherPaySuccess(type: String?) {
        Log.e("Tag","type="+type)
        if ("1"==type){//购买会员的他人支付申请成功
            val intent = Intent(mContext, MainActivity::class.java)
            intent.putExtra(Constants.KEY_INTENT_MAIN,2)
            startActivity(intent)
        }else{
            startActivity(MainActivity::class.java)
        }
    }
}